package ex.com.jm.food.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ex.com.jm.food.dao.AdminNoticeDao;
import ex.com.jm.food.entity.Notices;

/**
 * 
 * @author zhaojw
 *
 */
public class AdminNoticeDaoImpl extends HibernateDaoSupport implements
		AdminNoticeDao {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Notices> getNotices(final int initPage,final int pageRow,final String title,
			final String s_date,final String e_date) {
		// TODO Auto-generated method stub
		Object notice=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from Notices notice where 1=1 ";
				if (title!=null&&!title.equals("")) {
					hql+=" and notice.noticeTitle like '%"+title+"%'";
				}
				if(s_date!=null&&!s_date.equals("")){
					
						hql+=" and notice.created>='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
						hql+=" and notice.created<='"+e_date+"'";
				}
				Query query=session.createQuery(hql).setFirstResult(initPage).setMaxResults(pageRow);
				return query.list();
			}
		});
		return (List<Notices>) notice;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public int noticeCount(final String title,final String s_date,final String e_date) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from Notices notice where 1=1 ";
				if (title!=null&&!title.equals("")) {
					hql+=" and notice.noticeTitle like '%"+title+"%'";
				}
				if(s_date!=null&&!s_date.equals("")){
					hql+=" and notice.created>='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
					hql+=" and notice.created<='"+e_date+"'";
				}
				Query query=session.createQuery(hql);
				return query.list().size();
			}
		});
		return count;
	}

	@Override
	public Object saveNotice(Notices notices) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().save(notices);
	}

	@Override
	public void updateNotice(Notices notices) {
		// TODO Auto-generated method stub
         this.getHibernateTemplate().update(notices);
	}

	@Override
	public Notices editNotices(String id) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(Notices.class, id);
	}

	@Override
	public void delNotice(String id) {
		// TODO Auto-generated method stub
         this.getHibernateTemplate().delete(this.getHibernateTemplate().get(Notices.class, id));
	}

}
